﻿using System.Drawing;
using System.Drawing.Drawing2D;

namespace DrawTools.Editor.CanvasFigures
{
  public class CanvasText : CanvasRectangle
  {
    public string Text { get; set; }
    public Color TextColor { get; set; }
    public int FontSize { get; set; }
    public bool IsItalic { get; set; }
    public bool IsBold { get; set; }
    public bool IsUnderline { get; set; }
    public StringAlignment TextAlignment { get; set; }

    public CanvasText(string text, int x, int y, int width, int height) : base(x, y, width, height)
    {
      Text = text;
      FontSize = 32;
      TextColor = Color.FromArgb(246, 149, 69);
      TextAlignment = StringAlignment.Center;
    }

    public override void Draw(Graphics graphics)
    {
      base.Draw(graphics);

      using (var fontBrush = new SolidBrush(TextColor))
      {
        Rectangle rect = GetNormalizedRectangle(Rectangle);

        var format = new StringFormat(StringFormat.GenericTypographic)
        {
          Alignment = TextAlignment,
          LineAlignment = StringAlignment.Center
        };

        var textPath = new GraphicsPath();
        textPath.AddString(Text, FontFamily.GenericSansSerif, (int)GetFontStyle(), FontSize, rect, format);

        //DrawFuzzyPath(graphics, path, BorderColor, distance: 0, alpha: 80, shadowWidth: 3, opaqueWidth: 0);
        DrawFuzzyPath(graphics, textPath, BorderColor, 0, 190, 2, 0);
        DrawFuzzyPath(graphics, textPath, BorderColor, 1, 150, 3, 0);
        DrawFuzzyPath(graphics, textPath, BorderColor, 2, 100, 8, 0);
        graphics.FillPath(fontBrush, textPath);
      }
    }

    private FontStyle GetFontStyle()
    {
      var style = FontStyle.Regular;

      if (IsBold)
        style = FontStyle.Bold;

      if (IsItalic)
        style = style | FontStyle.Italic;

      if (IsUnderline)
        style = style | FontStyle.Underline;

      return style;
    }
  }
}